package mesweb.mesweb.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.MesTechnologyFlowCircleTreeNode;
import mesweb.mesweb.Utils.MesTechnologyFlowJsonTree;
import mesweb.mesweb.Utils.MesTechnologyFlowTableTree;
import mesweb.mesweb.entity.MesTechnologyFlow;
import mesweb.mesweb.service.IMesTechnologyFlowService;
import mesweb.mesweb.vo.MesTechnologyFlowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 工艺流程
 * @Author: jeecg-boot
 * @Date:   2021-06-18
 * @Version: V1.0
 */

@Api(tags="工艺流程")
@RestController
@RequestMapping("/mes/mesTechnologyFlow")
public class MesTechnologyFlowController {
	@Autowired
	private IMesTechnologyFlowService mesTechnologyFlowService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("工艺流程类型Id") @RequestParam( value = "technologyFlowTypeId",required = false) Long technologyFlowTypeId) {
        String result;
        try {
            result = (new MesTechnologyFlowJsonTree()).Tree(technologyFlowTypeId == null ? 0L: technologyFlowTypeId );
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        String newStr = result.replaceAll("\\\\","");
        MesTechnologyFlowCircleTreeNode obj =  JSON.parseObject(newStr, MesTechnologyFlowCircleTreeNode.class);
        return CommonResult.success(obj);

    }

	@GetMapping("/GetTreeData")
	@ApiOperation("获取全部数据列表")
	public CommonResult<?> GetTreeData(@ApiParam("工艺流程类型Id") @RequestParam( value = "technologyFlowTypeId",required = false, defaultValue = "0") Long technologyFlowTypeId,@ApiParam("父工艺流程Id") @RequestParam( value = "parentId",required = false,defaultValue = "0") Long parentId) {
		String result;
		try {
			result = (new MesTechnologyFlowTableTree()).Tree(technologyFlowTypeId,parentId);
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.failed(e.getMessage());
		}
		String newStr = result.replaceAll("\\\\","");
		MesTechnologyFlowCircleTreeNode obj =  JSON.parseObject(newStr, MesTechnologyFlowCircleTreeNode.class);
		return CommonResult.success(obj);

	}

	/**
	 * 分页列表查询
	 *
	 * @param mesTechnologyFlow
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="工艺流程-分页列表查询", notes="工艺流程-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesTechnologyFlow mesTechnologyFlow,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<MesTechnologyFlowVo> queryWrapper = new QueryWrapper<>();
		if(null != mesTechnologyFlow &&  null != mesTechnologyFlow.getTechnologyFlowTypeId() &&mesTechnologyFlow.getTechnologyFlowTypeId() > 0){
			queryWrapper.eq("a.technology_flow_type_id",mesTechnologyFlow.getTechnologyFlowTypeId());
		}
		Page<MesTechnologyFlowVo> pageMesTechnologyFlowVo = new Page<MesTechnologyFlowVo>(page, size);
		IPage<MesTechnologyFlowVo> pageList = mesTechnologyFlowService.getTechnologyFlow(pageMesTechnologyFlowVo, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesTechnologyFlow
	 * @return
	 */
	@ApiOperation(value="工艺流程-编辑", notes="工艺流程-编辑")
	@PostMapping(value = "/save")
	@Transactional
	public CommonResult<?> edit(@RequestBody MesTechnologyFlow mesTechnologyFlow) {

       boolean result = false;
        if(mesTechnologyFlow.getId() == 0L) {
			QueryWrapper<MesTechnologyFlow> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("technology_flow_no",mesTechnologyFlow.getTechnologyFlowNo());
			queryWrapper.or().eq("technology_flow_name",mesTechnologyFlow.getTechnologyFlowName());
			MesTechnologyFlow mesTechnologyFlow1 = mesTechnologyFlowService.getOne(queryWrapper);
			if(mesTechnologyFlow1 != null){
				String msg = StrUtil.format("编号{}或名称{}已经存在！",mesTechnologyFlow1.getTechnologyFlowNo(),mesTechnologyFlow1.getTechnologyFlowName());
				throw new ApiException(msg);
			}
             result = mesTechnologyFlowService.save(mesTechnologyFlow);
             //修改工艺流程的标准工时(所有所属工序的标准工时之和）
             mesTechnologyFlowService.updateWorkHours(mesTechnologyFlow.getId());
        }
        else
        {
            result = mesTechnologyFlowService.updateById(mesTechnologyFlow);
        }
        return CommonResult.success(mesTechnologyFlow);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺流程-通过id删除", notes="工艺流程-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesTechnologyFlowService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="工艺流程-批量删除", notes="工艺流程-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesTechnologyFlowIDs") @RequestBody List<Long> ids) {
		this.mesTechnologyFlowService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺流程-通过id查询", notes="工艺流程-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesTechnologyFlow mesTechnologyFlow = mesTechnologyFlowService.getById(id);
		return CommonResult.success(mesTechnologyFlow);
	}
}
